<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Layout
{

    var $obj;
    var $layout;
    var $data;

    function __construct($layout = "")
    {
        $this->obj =& get_instance();
        $this->layout = $layout;
    }

    function setLayout($layout, $data = array())
    {
        $this->layout = $layout;
        $this->data = $data;
    }


    function view($view, $data = null, $return = false)
    {
        $cats = $this->obj->category->get_all();
        $data['cats'] = $cats;
        if ($this->obj->session->userdata('logged_in')) {
            $session_data = $this->obj->session->userdata('logged_in');
            $data['is_logged_in'] = true;
            $data['user_name'] = $session_data['name'];
            $data['user_email'] = $session_data['email'];
            $data['user_id'] = $session_data['id'];
            $data['user_permission'] = $session_data['permission'];
            $data['hasPurchased'] = $this->obj->product->hasPurchased($session_data['id']);
            $data['hasCensored'] = $this->obj->product->hasCensored($session_data['id']);
            $data['hasPending'] = $this->obj->product->hasPending($session_data['id']);
        } else {
            $data['is_logged_in'] = false;
            $data['user_permission'] = null;
        }

        //if ($this->data) {
        $data = array_merge($data, $this->data);
        //}
        $loadedData = array();
        $loadedData['content_for_layout'] = $this->obj->load->view($view, $data, true);

        if ($return) {
            $output = $this->obj->load->view($this->layout, $loadedData, true);
            return $output;
        } else {
            $this->obj->load->view($this->layout, $loadedData, false);
        }
    }

}